package game.module.user.dao;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public class LanSessionCache {

	private static Logger logger = LoggerFactory.getLogger(LanSessionCache.class);

	static class SingletonHolder {
		static LanSessionCache instance = new LanSessionCache();
	}

	public static LanSessionCache getInstance() {
		return SingletonHolder.instance;
	}

	private Cache<Long, Integer> sessionCache = CacheBuilder.newBuilder().concurrencyLevel(8)
			.expireAfterAccess(3, TimeUnit.HOURS).build();
	
	private Cache<Integer, Long> sessionCache2 = CacheBuilder.newBuilder().concurrencyLevel(8)
			.expireAfterAccess(3, TimeUnit.HOURS).build();

	public Integer getCachePlayerId(Long sessionId) {
		return sessionCache.getIfPresent(sessionId);
	}
	
	public Long getSessionIdByPlayer(Integer playerId) {
		Long mySessionId = sessionCache2.getIfPresent(playerId);
		if(mySessionId != null) {
			sessionCache.getIfPresent(mySessionId);
		}
		return mySessionId;
	}

	public void removeSession(Long sessionId) {
		sessionCache.invalidate(sessionId);
	}

	public void addSession(Long sessionId, Integer playerId) {
		sessionCache.put(sessionId, playerId);
		sessionCache2.put(playerId, sessionId);
	}

}
